package com.dingding.gmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.ware.dao.WareSkuDao;
import com.dingding.gmall.ware.entity.WareSkuEntity;
import com.dingding.gmall.ware.service.WareSkuService;
import com.dingding.gmall.ware.vo.SkuHasStockVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("sku_id",skuId);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVO> getSkusHasStock(List<Long> skuIds) {
        if (CollectionUtils.isNotEmpty(skuIds)){
            return skuIds.stream().map(skuId -> {
                SkuHasStockVO skuHasStockVO = new SkuHasStockVO();
                //查询当前sku的总库存量
                Long count = baseMapper.getSkuStock(skuId);
                skuHasStockVO.setSkuId(skuId);
                Boolean result = count != null && count > 0;
                skuHasStockVO.setHasStock(result);
                return skuHasStockVO;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}